package itpenguin.portal;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class PortalServlet extends HttpServlet {
	
	public static String content;
	public static String mobileContent;
	public static Date published;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		if (! req.getRequestURI().equals("/")) {
			resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
			resp.setHeader("Location","/");
			return;
		}
		
		if (content == null) {
			UpdateServlet us = new UpdateServlet();
			us.doGet(req, resp);
		}

		boolean isMobile = req.getHeader("User-Agent").matches(".*(iPhone|Android).*");
		
		resp.setCharacterEncoding("UTF-8");
		resp.setHeader("Content-Type","text/html; charset=utf-8");
		resp.addDateHeader("Date", (new Date()).getTime());
		resp.addDateHeader("Last-Modified", published.getTime());
		PrintWriter out = resp.getWriter();
		out.write(isMobile ? mobileContent : content);
	
	}
}
